/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */

package com.huawei.fastble.adapter;

import com.huawei.fastble.ResourceTable;

import com.clj.fastble.data.BleDevice;

import ohos.agp.components.*;
import ohos.app.Context;
import ohos.bluetooth.ble.GattCharacteristic;
import ohos.bluetooth.ble.GattService;

import java.util.ArrayList;
import java.util.List;

/**
 * serviceItemProvider extends BaseItemProvider
 *
 */
public class CharacterItemProvider extends BaseItemProvider {
    private Context context;

    private List<GattCharacteristic> characteristics;

    /**
     * propertiesStrs
     */
    public List<String> propertiesStrs;

    public CharacterItemProvider(Context context, List<GattCharacteristic> itemList) {
        this.context = context;
        characteristics = itemList;
        propertiesStrs = new ArrayList<>();
    }

    private HandleOnclickListener handleOnclickListener;

    /**
     * setHandleOnclickListener
     * @param handleOnclickListener handleOnclickListener
     */
    public void setHandleOnclickListener(HandleOnclickListener handleOnclickListener) {
        this.handleOnclickListener = handleOnclickListener;
    }

    @Override
    public int getCount() {
        return characteristics.size();
    }

    @Override
    public Object getItem(int position) {
        return characteristics.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        return getRootView(position);
    }

    private Component getRootView(int position) {
        Component rootView =
                LayoutScatter.getInstance(context).parse(ResourceTable.Layout_character_item_layout, null, false);
        Text txtTitle = (Text) rootView.findComponentById(ResourceTable.Id_txt_title);
        Text txtUUID = (Text) rootView.findComponentById(ResourceTable.Id_txt_uuid);
        Text txtType = (Text) rootView.findComponentById(ResourceTable.Id_txt_type);
        Image img = (Image) rootView.findComponentById(ResourceTable.Id_img_arrow);

        GattCharacteristic characteristic = characteristics.get(position);

        // 制造商
        txtTitle.setText("服务：" + position);
        txtUUID.setText("UUID:" + characteristic.getUuid().toString());
        StringBuilder property = new StringBuilder();
        int charaProp = characteristic.getProperties();
        if ((charaProp & GattCharacteristic.PROPERTY_READ) > 0) {
            property.append("Read");
            property.append(" , ");
        }
        if ((charaProp & GattCharacteristic.PROPERTY_WRITE) > 0) {
            property.append("Write");
            property.append(" , ");
        }
        if ((charaProp & GattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0) {
            property.append("Write No Response");
            property.append(" , ");
        }
        if (property.length() > 1) {
            property.delete(property.length() - 2, property.length() - 1);
        }
        if (property.length() > 0) {
            txtType.setText("特性：" + "( " + property.toString() + ")" + "properties=" + charaProp);
            img.setVisibility(Component.VISIBLE);
            propertiesStrs.add(property.toString());
        } else {
            img.setVisibility(Component.HIDE);
            propertiesStrs.add("0");
        }

        return rootView;
    }

    /**
     * HandleOnclickListener
     */
    public interface HandleOnclickListener {
        /**
         * click
         * @param bleDevice BleDevice
         */
        void click(BleDevice bleDevice);
    }
}
